/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.Loaisanpham;
import POJO.Tinhtrangdonhang;
import UTIL.HibernateUtil;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author nhlvu07db373
 */
public class TinhtrangdonhangDAO extends HibernateDaoSupport{
    private Session session;

    /**
     *Rút trích danh sách tất cả loại tình trạng
     *
     */
    public List dsTinhtrangdonhangAll() {
        session = HibernateUtil.getSessionFactory().openSession();
        List list = null;
        try {
            org.hibernate.Transaction ts = session.beginTransaction();
            Criteria crit = session.createCriteria(Tinhtrangdonhang.class);
            list = crit.list();
            ts.commit();
            session.flush();
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            System.err.println(ex.getMessage().toString());
        } finally {
            session.close();
        }
        return list;
    }

    /**
     * Lấy thông tin tình trạng
     * @param maLoaiNguoiDung mã tình trạng
     * @return 
     */
    public Tinhtrangdonhang layTinhtrangdonhang(int matT) {

        Tinhtrangdonhang ttdh = null;
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            org.hibernate.Transaction ts = session.beginTransaction();
            Criteria crit = session.createCriteria(Tinhtrangdonhang.class).add(Restrictions.idEq(matT));
            List list = crit.list();
            ttdh= (Tinhtrangdonhang) list.get(0);
            ts.commit();
            session.flush();
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            System.err.println(ex.getMessage().toString());
        } finally {
            session.close();
        }
        return ttdh;
    }

    /**
     *Thêm/cập nhật tình trạng
     * @param  tt tình trạng
     */
    public boolean luuTinhtrangdonhang(Tinhtrangdonhang tt) {
        boolean kq = false;
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            org.hibernate.Transaction ts = session.beginTransaction();
            session.saveOrUpdate(tt);
            ts.commit();
            session.flush();
            kq = true;
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            System.err.println(ex.getMessage().toString());
        } finally {
            session.close();
        }
        return kq;
    }
}
